/**
 * @file    diag_temp.c
 * @brief   温度诊断
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-19
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-19 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "diag_temp.h"
#include "diagnosis.h"
#include "param_mng.h"
#include "sample.h"
#include "soc_calc.h"
#include "event_mng.h"
#include "var_mng.h"
#include "cfg_prj.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
static diag_data_type diag_otc_data = {0};
static diag_data_type diag_utc_data = {0};
static diag_data_type diag_otd_data = {0};
static diag_data_type diag_utd_data = {0};
static diag_data_type diag_tdiff_data = {0};
static diag_data_type diag_ote_data = {0};
static diag_data_type diag_ute_data = {0};
static diag_data_type diag_otm_data = {0};

extern uint8_t wifi_send_flag;
/**
 * @brief  充电过温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_otc(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	float smp_val = var_get_data(VAR_ID_MAX_TEMP_VAL) * 0.1 - 50;
	diag_data_type *p_data = &diag_otc_data;
	uint16_t otc_times = 0;
	double now_curr = 0;
    uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTC_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTC_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_OTC_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTC_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTC_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_OTC_PRP_DLY);
	now_curr = var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if((CURR_DR_CHG == sys_state) && (smp_val >= diag_para.alm_param))
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
                p_data->alm_time_cnt = 0;
				event_add_to_sram(EVT_ID_ALM_OTC , var_get_data(VAR_ID_MAX_TEMP_VAL) , param_get_data(PAR_ID_OTC_ALM_VAL));
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_DSG == sys_state) && (-now_curr > CHG_RECV_CURR))  
			|| (smp_val <= diag_para.alm_recv))
        {
            p_data->alm_state = 0;
            p_data->alm_time_cnt = 0;
        }
    }
    diag_set_alarm(ALM_ID_OTC , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if((CURR_DR_CHG == sys_state) && (smp_val >= diag_para.prp_param))
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				otc_times = param_get_data(PAR_ID_OTC_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OTC_PRP_TIMES , otc_times);
				event_add_to_sram(EVT_ID_PRP_OTC , var_get_data(VAR_ID_MAX_TEMP_VAL) , param_get_data(PAR_ID_OTC_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_DSG == sys_state) && (-now_curr > CHG_RECV_CURR))
			|| (smp_val <= diag_para.prp_recv))
        {
            p_data->prp_state = 0;
            p_data->prp_time_cnt = 0;		
        }		
    }

    diag_set_prp(PRP_ID_OTC , p_data->prp_state);
}

/**
 * @brief  充电欠温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_utc(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_utc_data;
	uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	uint16_t utc_times = 0;
	float smp_val = var_get_data(VAR_ID_MIN_TEMP_VAL) * 0.1 - 50;
	double now_curr = 0;
	
	now_curr = var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTC_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTC_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_UTC_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTC_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTC_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_UTC_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if((CURR_DR_CHG == sys_state) && (smp_val <= diag_para.alm_param))
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_UTC , var_get_data(VAR_ID_MIN_TEMP_VAL) , param_get_data(PAR_ID_UTC_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
		if(((CURR_DR_DSG == sys_state) && (-now_curr > CHG_RECV_CURR))
			|| (smp_val >= diag_para.alm_recv))
        {
            p_data->alm_state = 0;
            p_data->alm_time_cnt = 0;
        }
    }
    diag_set_alarm(ALM_ID_UTC , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if((CURR_DR_CHG == sys_state) && (smp_val <= diag_para.prp_param))
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
                p_data->prp_time_cnt = 0;
				utc_times = param_get_data(PAR_ID_UTC_PRP_TIMES) + 1;
				param_set_data(PAR_ID_UTC_PRP_TIMES , utc_times);
				event_add_to_sram(EVT_ID_PRP_UTC , var_get_data(VAR_ID_MIN_TEMP_VAL) , param_get_data(PAR_ID_UTC_PRP_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_DSG == sys_state) && (-now_curr > CHG_RECV_CURR))
			|| (smp_val >= diag_para.prp_recv))
        {
            p_data->prp_state = 0;
            p_data->prp_time_cnt = 0;		
        }		
    }

    diag_set_prp(PRP_ID_UTC , p_data->prp_state);
}

/**
 * @brief  放电过温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_otd(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_otd_data;
	uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	uint16_t otd_times = 0;
	float smp_val = var_get_data(VAR_ID_MAX_TEMP_VAL) * 0.1 - 50;
	float now_curr = 0;
	
	now_curr = var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTD_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTD_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_OTD_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTD_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTD_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_OTD_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if((CURR_DR_DSG == sys_state) && (smp_val >= diag_para.alm_param))
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_OTD , var_get_data(VAR_ID_MAX_TEMP_VAL) , param_get_data(PAR_ID_OTD_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_CHG == sys_state) && (now_curr >= DSG_RECV_CURR))
			|| (smp_val <= diag_para.alm_recv))
        {
            p_data->alm_state = 0;
            p_data->alm_time_cnt = 0;
        }
    }
    diag_set_alarm(ALM_ID_OTD , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if((CURR_DR_DSG == sys_state) && (smp_val >= diag_para.prp_param))
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				otd_times = param_get_data(PAR_ID_OTD_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OTD_PRP_TIMES , otd_times);
				event_add_to_sram(EVT_ID_PRP_OTD , var_get_data(VAR_ID_MAX_TEMP_VAL) , param_get_data(PAR_ID_OTD_PRP_VAL));
                p_data->prp_time_cnt = 0;
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_CHG == sys_state) && (now_curr >= DSG_RECV_CURR))
			|| (smp_val <= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
            p_data->prp_time_cnt = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);		
        }		
    }

    diag_set_prp(PRP_ID_OTD , p_data->prp_state);
}

/**
 * @brief  放电低温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_utd(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_utd_data;
	uint16_t utd_times = 0;
	uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	float smp_val = var_get_data(VAR_ID_MIN_TEMP_VAL) * 0.1 - 50;
	double now_curr = 0;
	
	now_curr = var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTD_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTD_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_UTD_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTD_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTD_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_UTD_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if((CURR_DR_DSG == sys_state) && (smp_val < diag_para.alm_param))
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_UTD , var_get_data(VAR_ID_MIN_TEMP_VAL) , param_get_data(PAR_ID_UTD_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_CHG == sys_state) && (now_curr >= DSG_RECV_CURR))
			|| (smp_val >= diag_para.alm_recv))
        {
            p_data->alm_state = 0;
            p_data->alm_time_cnt = 0;
        }
    }
    diag_set_alarm(ALM_ID_UTD , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if((CURR_DR_DSG == sys_state) && (smp_val <= diag_para.prp_param))
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				utd_times = param_get_data(PAR_ID_UTD_PRP_TIMES) + 1;
				param_set_data(PAR_ID_UTD_PRP_TIMES , utd_times);
				event_add_to_sram(EVT_ID_PRP_UTD , var_get_data(VAR_ID_MIN_TEMP_VAL) , param_get_data(PAR_ID_UTD_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if(((CURR_DR_CHG == sys_state) && (now_curr >= DSG_RECV_CURR))
			|| (smp_val >= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
            p_data->prp_time_cnt = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);		
        }	
    }

    diag_set_prp(PRP_ID_UTD , p_data->prp_state);
}

/**
 * @brief  温差诊断 
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_tdiff(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_tdiff_data;
	float smp_val = (var_get_data(VAR_ID_MAX_TEMP_VAL) - var_get_data(VAR_ID_MIN_TEMP_VAL)) * 0.1;
	
    diag_para.alm_param = param_get_data(PAR_ID_TDIFF_ALM_VAL) * 0.1;
    diag_para.alm_recv = param_get_data(PAR_ID_TDIFF_ALM_RCV) * 0.1;
    diag_para.alm_dly = param_get_data(PAR_ID_TDIFF_ALM_DLY);

    diag_para.prp_param = param_get_data(PAR_ID_TDIFF_PRP_VAL) * 0.1;
    diag_para.prp_recv = param_get_data(PAR_ID_TDIFF_PRP_RCV) * 0.1;
    diag_para.prp_dly = param_get_data(PAR_ID_TDIFF_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if(smp_val >= diag_para.alm_param)
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_TDIFF , smp_val * 10 , param_get_data(PAR_ID_TDIFF_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(smp_val <= diag_para.alm_recv)
        {
            p_data->alm_state = 0;
        }
    }
    diag_set_alarm(ALM_ID_TDIFF , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if(smp_val >= diag_para.prp_param)
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				event_add_to_sram(EVT_ID_PRP_TDIFF , smp_val * 10 , param_get_data(PAR_ID_TDIFF_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if((smp_val <= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);
        }		
    }

    diag_set_prp(PRP_ID_TDIFF , p_data->prp_state);
}

/**
 * @brief  环境高温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_ote(uint16_t cycle)
{   
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_ote_data;
	float smp_val = var_get_data(VAR_ID_ENV_TEMP) * 0.1 - 50;
	
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTE_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTE_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_OTE_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTE_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTE_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_OTE_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if(smp_val >= diag_para.alm_param) 
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_OTE , var_get_data(VAR_ID_ENV_TEMP) , param_get_data(PAR_ID_OTE_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(smp_val <= diag_para.alm_recv)
        {
            p_data->alm_state = 0;
        }
    }
    diag_set_alarm(ALM_ID_OTE , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if(smp_val >= diag_para.prp_param) 
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				event_add_to_sram(EVT_ID_PRP_OTE , var_get_data(VAR_ID_ENV_TEMP) , param_get_data(PAR_ID_OTE_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {

	    if((smp_val <= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);
        }
    }

    diag_set_prp(PRP_ID_OTE , p_data->prp_state);
}

/**
 * @brief  环境低温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_ute(uint16_t cycle)
{   
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_ute_data;
	float smp_val = var_get_data(VAR_ID_ENV_TEMP) * 0.1 - 50;
	
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTE_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTE_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_UTE_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTE_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_UTE_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_UTE_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if((smp_val <= diag_para.alm_param) && (smp_val * 10 != INVALID_TEMP))
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_UTE , var_get_data(VAR_ID_ENV_TEMP) , param_get_data(PAR_ID_UTE_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(smp_val >= diag_para.alm_recv)
        {
            p_data->alm_state = 0;
        }
    }
    diag_set_alarm(ALM_ID_UTE , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if((smp_val <= diag_para.prp_param) && (smp_val * 10 != INVALID_TEMP))
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				event_add_to_sram(EVT_ID_PRP_UTE , var_get_data(VAR_ID_ENV_TEMP) , param_get_data(PAR_ID_UTE_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if((smp_val >= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);
        }
    }

    diag_set_prp(PRP_ID_UTE , p_data->prp_state);
}

/**
 * @brief  MOS高温诊断
 * @param  cycle  : 时间周期
 * @param  sys_state : 系统状态
 * @param  smp_val : 采样值
 */
void diag_temp_otm(uint16_t cycle)
{
    diag_para_type diag_para = {0};
	diag_data_type *p_data = &diag_otm_data;
	float smp_val = var_get_data(VAR_ID_MOS_TEMP) * 0.1 - 50;
	
    diag_para.alm_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTM_ALM_VAL));
    diag_para.alm_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTM_ALM_RCV));
    diag_para.alm_dly = param_get_data(PAR_ID_OTM_ALM_DLY);

    diag_para.prp_param = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTM_PRP_VAL));
    diag_para.prp_recv = param_convert_float(PARAM_TEMP , param_get_data(PAR_ID_OTM_PRP_RCV));
    diag_para.prp_dly = param_get_data(PAR_ID_OTM_PRP_DLY);
    /* 告警诊断*/
    if(0 == p_data->alm_state)
    {
        if(smp_val >= diag_para.alm_param)
        {
            p_data->alm_time_cnt += cycle;
            if(p_data->alm_time_cnt >= diag_para.alm_dly) 
            {
                p_data->alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_OTM , var_get_data(VAR_ID_MOS_TEMP) , param_get_data(PAR_ID_OTM_ALM_VAL));
                p_data->alm_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->alm_time_cnt = 0;
        }
    }
    else
    {
        if(smp_val <= diag_para.alm_recv)
        {
            p_data->alm_state = 0;
        }
    }
    diag_set_alarm(ALM_ID_OTM , p_data->alm_state);
    /* 保护诊断 */
    if(0 == p_data->prp_state)
    {
        if(smp_val >= diag_para.prp_param)
        {
            p_data->prp_time_cnt += cycle;
            if(p_data->prp_time_cnt >= diag_para.prp_dly) 
            {
                p_data->prp_state = 1;
				event_add_to_sram(EVT_ID_PRP_OTM , var_get_data(VAR_ID_MOS_TEMP) , param_get_data(PAR_ID_OTM_PRP_VAL));
                p_data->prp_time_cnt = 0;
				
				wifi_send_flag = 1;
            }   
        }
        else
        {
            p_data->prp_time_cnt = 0;
        }
    }
    else
    {
        if((smp_val <= diag_para.prp_recv)||(1 == var_get_data(VAR_ID_FAULT_CLEAR_STATE)))
        {
            p_data->prp_state = 0;
			var_set_data(VAR_ID_FAULT_CLEAR_STATE , 0);
        }		
    }

    diag_set_prp(PRP_ID_OTM , p_data->prp_state);
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
